﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TISSEAN
{
    [DialogAttribute(Prefix = "FMN_")]
    public partial class FalseNearestDialog : Form
    {
        const long partitions = 16;
        const long corrLength = 20;
        const long length = long.MaxValue;
        const long exclude = 0;
        const int column = 1;
        const long minEmbed = 1;
        const long maxEmbed = 5;
        const double ratioFactor = 2.0;
        const long theiler = 0;

        public string FileName
        {
            get
            {
                return tbFileName.Text;
            }
            set
            {
                tbFileName.Text = value;
            }
        }

        public string Separator
        {
            get
            {
                return cbSeparator.SelectedItem.ToString();
            }
            set
            {
                cbSeparator.SelectedItem = value;
            }
        }

        public long Length
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbPoints.Text))
                {
                    return length;
                }
                else
                {
                    if (long.TryParse(tbPoints.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of points must long type");
                    }
                }
            }
            set
            {
                tbPoints.Text = value.ToString();
            }
        }

        public long Exclude
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbExclude.Text))
                {
                    return exclude;
                }
                else
                {
                    if (long.TryParse(tbExclude.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of lines to be ignored must long type");
                    }
                }
            }
            set
            {
                tbExclude.Text = value.ToString();
            }
        }

        public int[] Column
        {
            get
            {
                int res = 0;

                if (string.IsNullOrEmpty(tbColumn.Text))
                {
                    return new int[] {column};
                }
                else
                {
                    string[] numbers = tbColumn.Text.Split(new char[] { ',' });

                    if (numbers.Length == 0)
                    {
                        return new int[] { column };
                    }

                    int[] cols = new int[numbers.Length];

                    int i = 0;

                    foreach (string item in numbers)
                    {
                        if (int.TryParse(item, out res))
                        {
                            cols[i] = res;
                        }
                        else
                        {
                            throw new Exception("Column numbers must be int type");
                        }

                        i++;
                    }

                    return cols;
                }
            }
            set
            {
                tbColumn.Text = value.ToString();
            }
        }

        public long MinimumEmbedding
        {
            get
            {
                if (string.IsNullOrEmpty(tbMinEmbed.Text))
                {
                    return minEmbed;
                }
                else
                {
                    long res;

                    if (long.TryParse(tbMinEmbed.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Minmal embedding must long type");
                    }
                }
            }
            set
            {
                tbMinEmbed.Text = value.ToString();
            }
        }

        public long MaximumEmbedding
        {
            get
            {
                if (string.IsNullOrEmpty(tbMaxEmbed.Text))
                {
                    return maxEmbed;
                }
                else
                {
                    long res;

                    if (long.TryParse(tbMaxEmbed.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Maximum embedding must long type");
                    }
                }
            }
            set
            {
                tbMaxEmbed.Text = value.ToString();
            }
        }

        public double RatioFactor
        {
            get
            {
                if (string.IsNullOrEmpty(tbRatioFactor.Text))
                {
                    return ratioFactor;
                }
                else
                {
                    double res;

                    if (double.TryParse(tbRatioFactor.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Ratio factor must double type");
                    }
                }
            }
            set
            {
                tbRatioFactor.Text = value.ToString();
            }
        }

        public long TheilerWindow
        {
            get
            {
                if (string.IsNullOrEmpty(tbTheilerWindow.Text))
                {
                    return theiler;
                }
                else
                {
                    long res;

                    if (long.TryParse(tbTheilerWindow.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Ratio factor must be long type");
                    }
                }
            }
            set
            {
                tbTheilerWindow.Text = value.ToString();
            }
        }

        public long Boxes
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbMinEmbed.Text))
                {
                    return partitions;
                }
                else
                {
                    if (long.TryParse(tbMinEmbed.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of boxes must long type");
                    }
                }
            }
            set
            {
                tbMinEmbed.Text = value.ToString();
            }
        }

        public long Delay
        {
            get
            {
                long res = 0;

                if (string.IsNullOrEmpty(tbMaxEmbed.Text))
                {
                    return corrLength;
                }
                else
                {
                    if (long.TryParse(tbMaxEmbed.Text, out res))
                    {
                        return res;
                    }
                    else
                    {
                        throw new Exception("Number of boxes must long type");
                    }
                }
            }
            set
            {
                tbMaxEmbed.Text = value.ToString();
            }
        }


        public FalseNearestDialog()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbFileName.Text) && cbSeparator.SelectedItem != null)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;

                this.Close();
            }
            else
            {
                MessageBox.Show("Data file name or separator was not set");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;

            this.Close();
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = false;
            dlg.ShowDialog();

            if (!string.IsNullOrEmpty(dlg.FileName))
            {
                tbFileName.Text = dlg.FileName;
            }

        }
    }
}
